﻿using System;
using System.Collections.Generic;
using System.Text;

namespace JYM.Common
{
    /// <summary>
    /// 数据转换
    /// </summary>
    public class ConvertHelper
    {
        #region 转换成int

        /// <summary>
        /// 以安全方式把值转成int
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>int</returns>
        public static int SafeToInt32(object p_Value, int p_DefaultValue)
        {
            int result;
            try
            {
                result = Convert.ToInt32(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成int
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>int</returns>
        public static int SafeToInt32(string p_Value, int p_DefaultValue)
        {
            object value = p_Value;
            return SafeToInt32(value, p_DefaultValue);
        }
        /// <summary>
        /// 以安全方式把值转成int,转换出错默认返回0
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>int</returns>
        public static int SafeToInt32(string p_Value)
        {
            return SafeToInt32(p_Value, 0);
        }

        /// <summary>
        /// 以安全方式把值转成int?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>int</returns>
        public static int? SafeToInt32_2(object p_Value, int? p_DefaultValue)
        {
            int? result;
            try
            {
                result = Convert.ToInt32(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成int?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>int</returns>
        public static int? SafeToInt32_2(string p_Value, int? p_DefaultValue)
        {
            object value = p_Value;
            return SafeToInt32_2(value, p_DefaultValue);
        }

        /// <summary>
        /// 以安全方式把值转成int?,转换出错默认返回null
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>int?</returns>
        public static int? SafeToInt32_2(string p_Value)
        {
            return SafeToInt32_2(p_Value, null);
        }
        #endregion

        #region 转换成DateTime

        /// <summary>
        /// 以安全方式把值转成DateTime
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>DateTime</returns>
        public static DateTime SafeToDateTime(object p_Value, DateTime p_DefaultValue)
        {
            DateTime result;
            try
            {
                result = Convert.ToDateTime(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }

        /// <summary>
        /// 以安全的方式把值转成DateTime
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DateFormat">日期格式：yyyyMMdd,yyyy-MM-dd,MM/dd/yyyy等</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns></returns>
        public static DateTime SafeToDateTime(string p_Value, string p_DateFormat, DateTime p_DefaultValue)
        {
            DateTime result;
            try
            {
                result = DateTime.ParseExact(p_Value.ToString(), p_DateFormat, null);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }

        /// <summary>
        /// 以安全方式把值转成DateTime
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>DateTime</returns>
        public static DateTime SafeToDateTime(string p_Value, DateTime p_DefaultValue)
        {
            object value = p_Value;
            return SafeToDateTime(value, p_DefaultValue);
        }
        /// <summary>
        /// 以安全方式把值转成DateTime,转换出错默认返回default(DateTime)
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>DateTime</returns>
        public static DateTime SafeToDateTime(string p_Value)
        {
            return SafeToDateTime(p_Value, default(DateTime));
        }
        /// <summary>
        /// 以安全方式把值转成DateTime?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>DateTime?</returns>
        public static DateTime? SafeToDateTime2(object p_Value, DateTime? p_DefaultValue)
        {
            DateTime? result;
            try
            {
                result = Convert.ToDateTime(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全的方式把值转成DateTime
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DateFormat">日期格式：yyyyMMdd,yyyy-MM-dd,MM/dd/yyyy等</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns></returns>
        public static DateTime? SafeToDateTime2(string p_Value, string p_DateFormat, DateTime? p_DefaultValue)
        {
            DateTime? result;
            try
            {
                result = DateTime.ParseExact(p_Value.ToString(), p_DateFormat, null);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }

        /// <summary>
        /// 以安全方式把值转成DateTime?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>DateTime</returns>
        public static DateTime? SafeToDateTime2(string p_Value, DateTime? p_DefaultValue)
        {
            object value = p_Value;
            return SafeToDateTime2(value, p_DefaultValue);
        }
        /// <summary>
        /// 以安全方式把值转成DateTime?,转换出错默认返回null
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>DateTime?</returns>
        public static DateTime? SafeToDateTime2(string p_Value)
        {
            return SafeToDateTime2(p_Value, null);
        }
        #endregion

        #region 转换成decimal

        /// <summary>
        /// 以安全方式把值转成decimal
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>decimal</returns>
        public static decimal SafeToDecimal(object p_Value, decimal p_DefaultValue)
        {
            decimal result;
            try
            {
                result = Convert.ToDecimal(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成decimal
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>decimal</returns>
        public static decimal SafeToDecimal(string p_Value, decimal p_DefaultValue)
        {
            object value = p_Value;
            return SafeToDecimal(value, p_DefaultValue);
        }

        /// <summary>
        /// 以安全方式把值转成decimal,转换出错默认返回0
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>decimal</returns>
        public static decimal SafeToDecimal(string p_Value)
        {
            return SafeToDecimal(p_Value, 0);
        }

        /// <summary>
        /// 以安全方式把值转成decimal?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>decimal</returns>
        public static decimal? SafeToDecimal2(object p_Value, decimal? p_DefaultValue)
        {
            decimal? result;
            try
            {
                result = Convert.ToDecimal(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成decimal?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>decimal</returns>
        public static decimal? SafeToDecimal2(string p_Value, decimal? p_DefaultValue)
        {
            object value = p_Value;
            return SafeToDecimal2(value, p_DefaultValue);
        }
        /// <summary>
        /// 以安全方式把值转成decimal?,转换出错默认返回null
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>decimal?</returns>
        public static decimal? SafeToDecimal2(string p_Value)
        {
            return SafeToDecimal2(p_Value, null);
        }

        #endregion

        #region 转换成double

        /// <summary>
        /// 以安全方式把值转成double
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>double</returns>
        public static double SafeToDouble(object p_Value, double p_DefaultValue)
        {
            double result;
            try
            {
                result = Convert.ToDouble(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成double
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>double</returns>
        public static double SafeToDouble(string p_Value, double p_DefaultValue)
        {
            object value = p_Value;
            return SafeToDouble(value, p_DefaultValue);
        }

        /// <summary>
        /// 以安全方式把值转成double,转换出错默认返回0
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>double</returns>
        public static double SafeToDouble(string p_Value)
        {
            return SafeToDouble(p_Value, 0);
        }

        /// <summary>
        /// 以安全方式把值转成double?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>double?</returns>
        public static double? SafeToDouble2(object p_Value, double? p_DefaultValue)
        {
            double? result;
            try
            {
                result = Convert.ToDouble(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成double?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>double</returns>
        public static double? SafeToDouble2(string p_Value, double? p_DefaultValue)
        {
            object value = p_Value;
            return SafeToDouble2(value, p_DefaultValue);
        }
        /// <summary>
        /// 以安全方式把值转成decimal?,转换出错默认返回null
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>double?</returns>
        public static double? SafeToDouble2(string p_Value)
        {
            return SafeToDouble2(p_Value, null);
        }

        #endregion

        #region 转换成float

        /// <summary>
        /// 以安全方式把值转成float
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>float</returns>
        public static float SafeToSingle(object p_Value, float p_DefaultValue)
        {
            float result;
            try
            {
                result = Convert.ToSingle(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成float
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>float</returns>
        public static float SafeToSingle(string p_Value, float p_DefaultValue)
        {
            object value = p_Value;
            return SafeToSingle(value, p_DefaultValue);
        }

        /// <summary>
        /// 以安全方式把值转成float,转换出错默认返回0
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>float</returns>
        public static float SafeToSingle(string p_Value)
        {
            return SafeToSingle(p_Value, 0);
        }

        /// <summary>
        /// 以安全方式把值转成float?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>float?</returns>
        public static float? SafeToSingle2(object p_Value, float? p_DefaultValue)
        {
            float? result;
            try
            {
                result = Convert.ToSingle(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成float?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>float</returns>
        public static float? SafeToSingle2(string p_Value, float? p_DefaultValue)
        {
            object value = p_Value;
            return SafeToSingle2(value, p_DefaultValue);
        }
        /// <summary>
        /// 以安全方式把值转成decimal?,转换出错默认返回null
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>float?</returns>
        public static float? SafeToSingle2(string p_Value)
        {
            return SafeToSingle2(p_Value, null);
        }

        #endregion

        #region 转换成long

        /// <summary>
        /// 以安全方式把值转成long
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>long</returns>
        public static long SafeToInt64(object p_Value, long p_DefaultValue)
        {
            long result;
            try
            {
                result = Convert.ToInt64(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成long
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>long</returns>
        public static long SafeToInt64(string p_Value, long p_DefaultValue)
        {
            object value = p_Value;
            return SafeToInt64(value, p_DefaultValue);
        }

        /// <summary>
        /// 以安全方式把值转成long,转换出错默认返回0
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>long</returns>
        public static long SafeToInt64(string p_Value)
        {
            return SafeToInt64(p_Value, 0);
        }

        /// <summary>
        /// 以安全方式把值转成long?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>long?</returns>
        public static long? SafeToInt64_2(object p_Value, long? p_DefaultValue)
        {
            long? result;
            try
            {
                result = Convert.ToInt64(p_Value);
            }
            catch
            {
                result = p_DefaultValue;
            }
            return result;
        }
        /// <summary>
        /// 以安全方式把值转成long?
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>long</returns>
        public static long? SafeToInt64_2(string p_Value, long? p_DefaultValue)
        {
            object value = p_Value;
            return SafeToInt64_2(value, p_DefaultValue);
        }
        /// <summary>
        /// 以安全方式把值转成decimal?,转换出错默认返回null
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <returns>long?</returns>
        public static long? SafeToInt64_2(string p_Value)
        {
            return SafeToInt64_2(p_Value, null);
        }

        #endregion

        #region 转换成bool

        /// <summary>
        /// 以安全方式把值转成bool
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>bool</returns>
        public static bool SafeToBoolean(object p_Value, bool p_DefaultValue)
        {
            bool result;
            try
            {
                result = Convert.ToBoolean(p_Value);
            }
            catch (FormatException err)
            {
                int intValue;
                if (int.TryParse(p_Value.ToString(), out intValue))
                    result = Convert.ToBoolean(intValue);
                else
                    result = p_DefaultValue;
            }
            catch
            {
                result = p_DefaultValue;
            }

            return result;
        }

        /// <summary>
        /// 以安全方式把值转成bool
        /// </summary>
        /// <param name="p_Value">要转换的值</param>
        /// <param name="p_DefaultValue">默认值</param>
        /// <returns>bool</returns>
        public static bool SafeToBoolean(string p_Value, bool p_DefaultValue)
        {
            object value = p_Value;
            return SafeToBoolean(value, p_DefaultValue);
        }

        #endregion

        /// <summary>
        /// 安全的转换Enum类型
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="text"></param>
        /// <returns></returns>
        public static TEnum SafeToEnum<TEnum>(string text, TEnum p_DefaultValue) where TEnum : struct
        {
            try
            {
                return EnumHelper.Parse<TEnum>(text, false, p_DefaultValue);
            }
            catch (ArgumentException)
            {
                return p_DefaultValue;
            }
        }

    }
}
